package com.springboot.multiplethread.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.springboot.multiplethread.service.MyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 服务接口实现类
 *
 * @author sam
 */
@Service
@Slf4j
public class MyServiceImpl implements MyService {

    @Resource(name = "taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    /**
     * 查询等业务逻辑，返回值需要为Future类型
     **/

    @Override
    @Async
    public Future<JSONObject> queryViaMultipleThread1() {
        JSONObject result = new JSONObject();
        result.put("data", "queryViaMultipleThread1");
        log.info("queryViaMultipleThread1");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
        return new AsyncResult<>(result);
    }

    @Override
    @Async
    public Future<JSONObject> queryViaMultipleThread2() {
        JSONObject result = new JSONObject();
        result.put("data", "queryViaMultipleThread2");
        log.info("queryViaMultipleThread2");
        return new AsyncResult<>(result);
    }

    @Override
    @Async
    public Future<JSONObject> queryViaMultipleThread3() {
        JSONObject result = new JSONObject();
        result.put("data", "queryViaMultipleThread3");
        log.info("queryViaMultipleThread3");
        return new AsyncResult<>(result);
    }

    @Override
    public Map<String, Object> queryOrder() {
        log.info("查询订单时间：{}", System.currentTimeMillis());
        try {
            // 假设耗时2s
            Thread.sleep(2000);
        } catch (Exception e) {
        }
        Map<String, Object> result = new HashMap<>();
        result.put("order", "获取到了订单信息");
        log.info("获取订单信息");
        return result;
    }

    @Override
    public Map<String, Object> queryInventory() {
        log.info("查询库存时间：{}", System.currentTimeMillis());
        try {
            // 假设查库存用了1s
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        Map<String, Object> result = new HashMap<>();
        result.put("inventory", "获取到了库存信息");
        log.info("获取库存信息");
        return result;
    }

    @Override
    public Map<String, Object> queryItems() {
        log.info("查询商品时间：{}", System.currentTimeMillis());
        try {
            // 假设查订单用了2.5s
            Thread.sleep(2500);
        } catch (Exception e) {

        }
        Map<String, Object> result = new HashMap<>();
        result.put("items", "获取到了商品信息");
        log.info("获取商品信息");
        return result;
    }

    @Override
    public Map<String, Object> queryAsyn() {
        long start = System.currentTimeMillis();

        Map<String, Object> data = new HashMap<>();
        // 查订单信息
        Future<Map<String, Object>> order = taskExecutor.submit(this::queryOrder);

        // 查库存信息
        Future<Map<String, Object>> inventory = taskExecutor.submit(this::queryInventory);

        // 查商品信息
        Future<Map<String, Object>> item = taskExecutor.submit(this::queryItems);

        // 整合数据
        try {
            data.put("inventory", inventory.get());
            data.put("item", item.get());
            data.put("order", order.get());
        } catch (Exception e) {
        }

        // 记录响应时间并返回数据
        log.info("接口耗时：{}", System.currentTimeMillis() - start);
        return data;
    }
}
